// 有关于投保的基类
import { ref, type Ref } from 'vue'
import * as apis from '@/apis/index'
import dayjs from 'dayjs'
// types.ts
export type ProductinfoParams = {
  productCode: string
  userId: string | number
}

class productinfoBaseClass {
  // 定义属性类型
  // 产品code
  public productCode: Ref<string>
  // 代理人userId
  public userId: Ref<string | number>
  // 头部信息资料
  public headContent: Ref<Record<string, any>>
  // 方案信息
  public resContent: Ref<any[]>
  // 代理人信息
  public sensorsInfo: Ref<Record<string, any>>
  // 方案价格
  public schemePrice: Ref<number>
  // 产品是否关闭
  public isProductClose: Ref<boolean>
  // 产品关闭原因
  public upgradeTip: Ref<string>
  // 跳转试算页面url 可以不用写
  public trialUrl: Ref<string>

  // 构造函数
  constructor({ productCode, userId }: ProductinfoParams) {
    this.productCode = ref(productCode)
    this.userId = ref(userId)

    this.headContent = ref({})
    this.resContent = ref([])
    this.sensorsInfo = ref({})
    this.schemePrice = ref(0)
    this.isProductClose = ref(false)
    this.upgradeTip = ref('')
    this.trialUrl = ref('')
  }

  // 初始化
  public async init() {
    await this.getProductDetail()
    await this.getSensorsInfo()
  }

  // 获取产品详情信息
  public async getProductDetail() {
    return new Promise((resolve) => {
      apis
        .getWLProductDetails({ mdProductCode: this.productCode.value })
        .then((data: Record<string, any>) => {
          document.title = data.productName || ''

          this.headContent.value = data
          this.resContent.value = data.programmeList || []
          this.schemePrice.value = this.resContent.value.length
            ? (this.resContent.value[0] as any).schemePrice
            : data.price

          this.judgeProductClose()
          this.upgradeTip = data.interceptPrompt || '' // 关闭原因

          //   localStorage.setItem('sensorsInfo', JSON.stringify(sensorsInfoNew))
          //   sessionStorage.setItem('sensorsInfo', JSON.stringify(sensorsInfoNew))
          //   this.$store.dispatch('common/updateSensorsInfo', sensorsInfoNew)

          //   let shareData = Object.assign(data, {
          //     linkUrl: window.location.href,
          //     url: window.location.href
          //   })
          //   localStorage.setItem('shareInfo', JSON.stringify(shareData))
          //   this.$store.dispatch('common/updateShareInfo', shareData)
          //   // 激活分享与跳转小程序标签
          //   this.$utils.fetchJsSdkSign(shareData)

          resolve(true)
        })
    })
  }

  // 判断产品是否已经关闭了
  private judgeProductClose() {
    let flag = false
    // 表示关闭
    if (this.headContent.value.interceptInsurance == 1) {
      const startTime = dayjs(this.headContent.value.interceptStartTime).valueOf()
      // 关闭截止时间，如果没有设置，表示是永久，设置几百年之后
      const interceptEndTime = this.headContent.value.interceptEndTime || '2999-12-20 23:59:59'
      const endTime = dayjs(interceptEndTime).valueOf()
      if (Date.now() >= startTime && Date.now() <= endTime) {
        flag = true
      }
    }
    this.isProductClose.value = flag
  }

  // 获取代理人详情信息
  public async getSensorsInfo() {
    return new Promise((resolve) => {
      apis
        .getSensorsData({
          userId: this.userId.value,
          productCode: this.productCode.value
        })
        .then((data: Record<string, any>) => {
          this.sensorsInfo.value = {
            ...data,
            productCode: this.productCode.value,
            userid: this.userId.value
          }
          resolve(true)
        })
    })
  }
}

// 导出基类
export default productinfoBaseClass
